Imports System.drawing

Namespace Overlay
    ''' <summary>
    ''' The BarOverlay object is used to chart non-stacked bar series. Series must be of type jChart.Overlay.Series.BarSeries
    ''' </summary>
    ''' <remarks></remarks>
    Public Class BarOverlay
        Inherits OverLayBase

        ''' <summary>
        ''' oChartDimensions is a jChart.ChartDimensions object that describes the interior of the chart. oXAxis and oYAxis are 
        ''' the X and Y axes that should be used for series belonging to the OverLay object.
        ''' </summary>
        ''' <param name="oChartDimensions"></param>
        ''' <param name="oXAxis"></param>
        ''' <param name="oYAxis"></param>
        ''' <remarks></remarks>
        Public Sub New( _
            ByVal oChartDimensions As ChartDimensions, _
            ByVal oXAxis As XAxis, _
            ByVal oYAxis As YAxis)

            MyBase.New(oChartDimensions, oXAxis, oYAxis)
        End Sub

        Protected Overrides Sub DrawAllSeries(ByRef g As Graphics)
            '---Count all series
            Dim iCountOfSeries_Bar As Integer = moSeries.Count

            Dim iCurrentBar As Integer = 0

            For Each oSeries As Series.BarSeries In moSeries
                SeriesToBar(g, oSeries, iCurrentBar, iCountOfSeries_Bar)
                iCurrentBar = iCurrentBar + 1
            Next
        End Sub

        Private Sub SeriesToBar( _
                        ByRef g As Graphics, _
                        ByVal oSeries As Series.BarSeries, _
                        ByVal iCurrentBar As Integer, _
                        ByVal iCountOfBarSeries As Integer)

            '---Define area values.
            Dim iXPointCount As Integer = moXAxis.XAxisValues.GetUpperBound(0)

            Dim oTopValues(iXPointCount) As Single
            Dim oBottomValues(iXPointCount) As Single

            For iAreaPoint As Integer = 0 To iXPointCount
                '---Bottom of area is top of previous area
                oBottomValues(iAreaPoint) = moYAxis.YAxisValues(0)

                '---Now fill in current area
                oTopValues(iAreaPoint) = oSeries.Values(iAreaPoint)
            Next

            Dim iBarWidth As Integer = (moXAxis.XAxisLabelBoxWidth / iCountOfBarSeries) - 8
            Dim iBarOffset As Integer = -1 * (moXAxis.XAxisLabelBoxWidth / 2) + (iBarWidth * iCurrentBar) + iBarWidth / 2 + 4

            Dim oRegionList As ArrayList = Nothing
            oRegionList = CalculateBarAreaRegions( _
                                                g, _
                                                oSeries.Name, _
                                                oTopValues, _
                                                oBottomValues, _
                                                iBarWidth, _
                                                iBarOffset)

            For Each oRegion As RegionArea In oRegionList
                If oRegion.YValue <> oSeries.NullValue Then
                    MyBase.AddPolygonRegion(oRegion)

                    Dim iOutlineAlpha As Integer
                    Dim iInteriorAlpha As Integer

                    If oSeries.AlphaValue < 205 Then
                        iOutlineAlpha = oSeries.AlphaValue + 50
                        iInteriorAlpha = oSeries.AlphaValue
                    Else
                        iOutlineAlpha = oSeries.AlphaValue
                        iInteriorAlpha = oSeries.AlphaValue - 50
                    End If

                    Dim oInteriorColor As Color = Color.FromArgb(iInteriorAlpha, oSeries.Color.R, oSeries.Color.G, oSeries.Color.B)
                    g.FillRegion(New SolidBrush(oInteriorColor), oRegion.SeriesRegion)

                    g.DrawPolygon(New Pen(oSeries.BorderColor, oSeries.BorderWidth), oRegion.SeriesPoints)

                    Dim oExteriorColor As Color = Color.FromArgb(iOutlineAlpha, oSeries.Color.R, oSeries.Color.G, oSeries.Color.B)
                    g.DrawPolygon(New Pen(oInteriorColor, 2), oRegion.SeriesPoints)
                End If
            Next
        End Sub
    End Class
End Namespace